package com.mingtu.dzpatrol.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.amap.api.maps.MapsInitializer;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2020/6/19 14:51:02
 * @Email : 1281573890@qq.com
 * @Eescription :
 */
public abstract class BaseFragment extends Fragment {
    private Unbinder unbinder;
    private View layout;
    public Context context;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //防止第二次加载的时候重复调用onCreateView(),重新new了一个pageAdaper导致子fragment不显示，显示空白
        if (layout != null) {
            ViewGroup parent = (ViewGroup) layout.getParent();
            if (parent != null) {
                parent.removeView(layout);
            }
            return layout;
        }
        layout = inflater.inflate(getLayoutId(), null);
        unbinder = ButterKnife.bind(this, layout);
        return layout;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //高德地图
        MapsInitializer.updatePrivacyShow(getActivity(),true,true);
        MapsInitializer.updatePrivacyAgree(getActivity(),true);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initView(layout);
        getData();
    }

    /**
     * 获取布局ID
     */
    protected abstract int getLayoutId();


    /**
     * 数据初始化操作
     */
    protected abstract void initData();

    /**
     * 初始化视图
     */
    protected abstract void initView(View view);

    /**
     * 请求数据
     */
    protected abstract void getData();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
